<template>
  <div class="container">
    <!-- 导航固定定位 fixed -->
    <van-nav-bar
      fixed
      title="搜索结果"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      @load="onLoad"
      finished-text="没有更多了"
    >
      <van-cell v-for="(item, i) in list" :key="i">
        <div class="article_item">
          <h3 class="van-ellipsis">
            {{ item.title }}
          </h3>
          <div class="img_box" v-if="item.cover.type === 3">
            <van-image class="w33" fit="cover" :src="item.cover.images[0]" />
            <van-image class="w33" fit="cover" :src="item.cover.images[1]" />
            <van-image class="w33" fit="cover" :src="item.cover.images[2]" />
          </div>
          <div class="img_box" v-if="item.cover.type === 1">
            <van-image class="w100" fit="cover" :src="item.cover.images[0]" />
          </div>
          <div class="info_box">
            <span>{{ item.aut_name }}</span>
            <span>{{ item.comm_count }}评论</span>
            <span>{{ item.pubdate | relTime }}</span>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { searchArticles } from '../../api/search'
export default {
  name: 'serach-result',
  data () {
    return {
      list: [],
      loading: false,
      finished: false
    }
  },
  created () {
    // 初始化默认=》查询条件数据(不是响应式)
    console.log(this.$route.query.q)
    this.query = {
      page: 1,
      q: this.$route.query.q
    }
  },
  methods: {
    async onLoad () {
      const { data } = await searchArticles(this.query)
      // console.log('后台返回的搜索结果：', status, data)
      this.list.push(...data.results)

      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (this.list.length === data.total_count) {
        this.finished = true
      } else {
        // 页码加一
        this.query.page++
      }
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding-top: 46px;
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      height: 180px;
      width: 100%;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    span {
      padding-right: 10px;
    }
  }
}
</style>
